<style lang="less">
  @import './login.less';
</style>

<template>
  <div class="login">
    <div class="login-con">
      <Card icon="log-in" title="欢迎登录" :bordered="false">
        <div class="form-con">
          <Form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit">
            <FormItem prop="username">
              <Input v-model="form.username" placeholder="请输入用户名">
                <span slot="prepend">
                  <Icon :size="16" type="ios-person"></Icon>
                </span>
              </Input>
            </FormItem>
            <FormItem prop="password">
              <Input type="password" v-model="form.password" placeholder="请输入密码">
                <span slot="prepend">
                  <Icon :size="14" type="md-lock"></Icon>
                </span>
              </Input>
            </FormItem>
            <FormItem prop="code" v-if="showKaptcha"  class="vcodeform">
              <Input v-model="form.code" size="large" placeholder="请输入验证码"  >
              <span slot="prepend">
                <Icon type="lock-combination"></Icon>
              </span>
              <span slot="append">
                <img :src="'/api/code/'+form.codeKey" @click="initVcode" style="height:20px">
              </span>
              </Input>
            </FormItem>
            <div style="text-align:right;"><a @click="goForgetPwd" >忘记密码？</a></div>

            <FormItem>
              <Button @click="handleSubmit" type="primary" long>登录</Button>
            </FormItem>

          </Form>
        </div>
      </Card>

    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
// import LoginForm from '_c/login-form';
import { mapActions } from 'vuex'
import store from '@/store/'
export default {
  // components: {
  //   LoginForm
  // },
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ]
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  data () {
    return {
      errNum: 0,
      form: {
        username: 'admin',
        password: '',
        codeKey: '',
        code: ''
      }
    }
  },
  computed: {
    showKaptcha () {
      // `this` 指向 vm 实例
      let result = this.$store.state.user.showKaptcha > 0
      // console.log(999,result);
      return result
    },
    rules () {
      return {
        username: this.userNameRules,
        password: this.passwordRules
      }
    }
  },
  methods: {
    ...mapActions([
      'handleLogin',
      'getUserInfo',
      'handleArea'
    ]),
    initVcode () {
      let rnd = Math.random().toString() + (new Date()).getTime()
      this.form.codeKey = rnd
    },

    goForgetPwd () {
      this.$router.push({ name: 'forgetPwd' })
    },
    handleSubmit () {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          let data = await this.handleLogin(this.form)
          
          if (data.code == 0) { // success
            
            let res = await this.getUserInfo()
            
            // console.log(res,'res')
            this.$store.dispatch('store_showKaptcha', null)
            await this.handleArea() // get area
            // console.log(this.handleArea(),'1111111')
            // console.log('homeName', this.$config, res)
            if (res.distid == '330101') {
              this.$router.push({ name: 'ROLE_check' })
            } else {
              this.$router.push({ name: this.$config.homeName })
            }
          } else {
            this.errNum++
            this.$Message.error(data.message)
            if (this.errNum === 3) { this.$store.dispatch('store_showKaptcha', 1) }
            this.errorTime++
            this.form.username = ''
            this.form.password = ''
            this.form.code = ''
            this.initVcode()
          }
        }
      })
    }

  },
  created () {
    this.initVcode()
  }
}
</script>

<style>

</style>
